import { Injectable } from '@angular/core';
import { Camera, CameraOptions } from '@ionic-native/camera/ngx';
import { Storage } from '../providers/Storage';


@Injectable({
  providedIn: 'root'
})
export class PhotoService {

  public photos: Photo[] = [];
   image:string;

  constructor(private camera: Camera) { }

  takePicture() {
    const options: CameraOptions = {
      quality: 100,
      destinationType: this.camera.DestinationType.DATA_URL,
      encodingType: this.camera.EncodingType.JPEG,
      mediaType: this.camera.MediaType.PICTURE
    }
    
    this.camera.getPicture(options).then((imageData) => {
      // Add new photo to gallery
      this.photos.unshift({
        data: 'data:image/jpeg;base64,' + imageData
      });
      this.image='data:image/jpeg;base64,' + imageData;
      Storage.sessionStorage.set('image', this.image);
      // Save all photos for later viewing
      Storage.sessionStorage.set('photos', this.photos);
    }, (err) => {
     // Handle error
     console.log("Camera issue: " + err);
    });

  }

  loadSaved() {
    Storage.sessionStorage.get('image').then((photos) => {
      // this.photos = photos || [];
      this.image=photos;
    });
  }

}

class Photo {
  data: any;
}
